/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.converters;

import edu.upn.sigemfi.ope.be.Boveda;
import edu.upn.sigemfi.vfi.bc.MonedaDelegate;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Alex
 */
public class BovedaConverter implements Converter{

    private MonedaDelegate delegado;
        @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
          if (arg2 == null) {
            return null;
        }
        try {
            Long id = Long.parseLong(arg2);
            return delegado.buscar(id);

        } catch (Exception e) {
            return null;
        }

    }

  @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Boveda) {
            Boveda mo = (Boveda) arg2;
            return mo.getIdBoveda().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo boveda");
        }
    }
    }